package com.znz.web;

import com.znz.domain.*;
import com.znz.service.OrderService;
import com.znz.util.common.Page;
import com.znz.util.common.ReturnObj;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * OrderController
 * 订单Controller
 * @author donglin Ni
 * @date 2015/4/26
 */
@RequestMapping("/order")
@Controller
public class OrderController extends AbstractController{

    @Autowired
    private OrderService orderService;

    private Logger logger = LoggerFactory.getLogger(OrderController.class);
    /**
     * 客服查看订单列表页
         * @return
     */
    @RequestMapping(value={"/","/list"})
    public ModelAndView orderList(String userName,Page page) {
       try{
           ModelAndView modelAndView=null;
           Map<String,Object> map=new HashMap<String, Object>();
           Long tcustomerId=1l;
           Page<Torder> torderPage=orderService.getTorderAllByTcustomer(tcustomerId,userName,page);
           map.put("torderPage",torderPage);
           modelAndView = getTemplate("order/list",map);
           return modelAndView;
       }catch (Exception e){
           logger.error("页面初始化异常!");
           ModelAndView modelAndView=getTemplate("uncaughtException",null);
           return modelAndView;
       }
    }
    /**
     * 医院查看订单列表页
     * @return
     */
    @RequestMapping(value="/hospital/list")
    public ModelAndView orderListForHospital(String userName,Page page) {
        try{
            ModelAndView modelAndView=null;
            Map<String,Object> map=new HashMap<String, Object>();
            Page<Torder> torderPage=orderService.getTorderAllByThospital(userName,page);
            map.put("torderPage",torderPage);
            modelAndView = getTemplate("order/hospital/list",map);
            return modelAndView;
        }catch (Exception e){
            logger.error("页面初始化异常!");
            ModelAndView modelAndView=getTemplate("uncaughtException",null);
            return modelAndView;
        }
    }
    /**
     * 新增订单
     * @return
     */
    @RequestMapping(value="/toAdd")
    private ModelAndView toAdd(Long cityId,Long province,String hospitalName,Page<Thospital> page) {
        try {
            //初始化下拉菜单数据
            Map<String,Object> map=new HashMap<String,Object>();
            //TODO 获取所有的部位
            List<Part> partArray=orderService.getPartAll();
            map.put("partArray",partArray);
            if(null!=partArray && partArray.size()>0){
                //TODO 根据部位ID获取所有项目
                List<Project> projectArray=orderService.getProjectByPartId(partArray.get(0).getId());
                map.put("projectArray",projectArray);
            }
           //TODO 根据省份ID和城市ID获取医院列表
            Page<Thospital> thospitalList=orderService.getHospitalAllByCityId(cityId,province,hospitalName,page);
            map.put("orderHospital",thospitalList.getRows());
            //TODO 需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("order/add",map);
            return modelAndView;
        } catch (Exception e) {
            logger.error("新增订单初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    /**
     * 订单详情
     * @return
     */
    @RequestMapping(value="/hospital/detail")
    private ModelAndView detail(Long orderId) {
        try {
            //初始化下拉菜单数据
            Map<String,Object> map=new HashMap<String,Object>();
            orderId=1l;
            if(null!=orderId){
               List<TorderHospitalProject> torderHospitalProjectList=TorderHospitalProject.getTorderHospitalProjectByOrderId(orderId);
               map.put("torderHospitalProjectList",torderHospitalProjectList);
            }
            //TODO 需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("order/hospital/detail",map);
            return modelAndView;
        } catch (Exception e) {
            logger.error("订单详情初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    /**
     * 修改订单
     * @param orderid
     * @return
     */
    @RequestMapping(value="/toEdit")
    private ModelAndView toEdit(Long orderid) {
        try {
            //TODO 这里需要根据医院id获取医院对象放到map里
            Map<String,Object> map=new HashMap<String,Object>();
            //TODO 同时还需要获取项目列表放到map里，用于配置项目列表
            ModelAndView modelAndView = getTemplate("order/edit",map);
            return modelAndView;
        }catch (Exception e){
            logger.error("修改订单页面初始化异常",e);
            ModelAndView modelAndView = getTemplate("uncaughtException",null);
            return  modelAndView;
        }
    }

    @RequestMapping(value = "/getPartAll")
    @ResponseBody
    public ReturnObj getPartAll(){
        ReturnObj returnObj=null;
        //TODO  获取所有的部位
        try {
            List<Part> partList=orderService.getPartAll();
            returnObj=ReturnObj.createSuccess(partList);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }

    @RequestMapping(value = "/getProjectByPartId")
    @ResponseBody
    public ReturnObj getProjectByPartId(@RequestParam Long partId){
        ReturnObj returnObj=null;
        //TODO 根据部位获取该部位所包含的项目接口
        try {
            List<Project> jsonArray=orderService.getProjectByPartId(partId);
            returnObj=ReturnObj.createSuccess(jsonArray);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }

    @RequestMapping(value = "/saveOrder")
    @ResponseBody
    public ReturnObj saveOrder(@Valid Torder  order){
        //TODO 保存订单
         ReturnObj returnObj=null;
		try {
		    Torder torder=orderService.addOrder(order);
			returnObj=ReturnObj.createSuccess(torder);
		} catch (Exception e) {
            e.printStackTrace();
			returnObj=ReturnObj.createError();
		}
		return   returnObj;
    }

    @RequestMapping(value = "/getHospitalAllByCityId")
    @ResponseBody
    public ReturnObj getHospitalAllByCityId(@RequestParam Long cityId,@RequestParam Long provinceid,String hospitalName){
        ReturnObj returnObj=null;
        //TODO 根据省份代码或者根据市区代码获取其下所有的医院
        try {
            List<Thospital> torderList=orderService.getHospitalAllByCityIdBytest(cityId,provinceid,hospitalName);
            returnObj=ReturnObj.createSuccess(torderList);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }

    @RequestMapping(value = "/getHospitalByLikeName")
    @ResponseBody
    public ReturnObj getHospitalByLikeName(@RequestParam String hospitalName,Page page){
         ReturnObj returnObj=null;
        //TODO 根据医院名称模糊查询医院
        try {
            List<Thospital> thospitalList=orderService.getHospitalByLikeName(hospitalName,page);
            returnObj=ReturnObj.createSuccess(thospitalList);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            returnObj=ReturnObj.createError();
        }
        return returnObj;
    }
   @RequestMapping(value = "/saveHospitalProject")
   @ResponseBody
   public  ReturnObj saveHospitalProject(@Valid Torder torder){
       ReturnObj returnObj=null;
       //TODO 保存订单分配的医院
       return  returnObj;
   };
   @RequestMapping(value = "/getProjectByOrderId")
   @ResponseBody
   public ReturnObj  getProjectByOrderId(Long orderId){
       //TODO 根据订单ID查询项目列表(查询订单详情)
       ReturnObj returnObj=null;
      try{
          orderId=1l;
          List<TorderHospitalProject> projectList=orderService.getProjectListbyOrderId(orderId);
          returnObj=ReturnObj.createSuccess(projectList);
      }catch (Exception e){
          e.printStackTrace();
          returnObj=ReturnObj.createError();
      }
       return returnObj;
   }

   @RequestMapping(value = "/getTorderAllByTcustomer")
   @ResponseBody
   public  ReturnObj  getTorderAllByTcustomer(String userName){
       //TODO 根据订单ID查询项目列表(查询订单详情)
       ReturnObj returnObj=null;
       try{
           Long tcustomerId=1l;
           List<Torder> list=orderService.getTorderAllByTcustomerTest(tcustomerId,userName);
           returnObj=ReturnObj.createSuccess(list);
       }catch (Exception e){
           e.printStackTrace();
           returnObj=ReturnObj.createError();
       }
       return returnObj;
   }
}

